"""
用户信息的API
@Time: 2021/1/28 15:08
@Author:
@File: user_controller.py
"""
from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field
from sqlalchemy.orm import Session

from models.dto import RESTfulModel
from dependencies import get_mysql_db, verify_token
from mapper.mysql_mapper import user_mapper

user_router = APIRouter(tags=['user'])


class UserDetail(BaseModel):
    """用户的详细信息"""
    account: str = Field(description='用户账号')
    name: str = Field(description='用户姓名')


@user_router.get("/user",
                  response_model=RESTfulModel[UserDetail],
                  summary='用户的详细信息',
                  description="根据给定的用户 username 获取该用户的全部信息"
                  )
async def get_user_detail(username: str = Depends(verify_token),
                          mysql_db: Session = Depends(get_mysql_db)):
    user = user_mapper.get_user(mysql_db, username)
    if not user:
        return RESTfulModel(code=-1, data='该用户不存在')
    return RESTfulModel(code=0, data=UserDetail(
        account=user.username,
        name=user.name,
    ))
